//
//  SendSuggestViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "SendSuggestViewController.h"

@interface SendSuggestViewController () <UITextViewDelegate>

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UITextView * textView;
@property (nonatomic, weak)UIButton * finishButton;

@end

@implementation SendSuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xE8E8E8);
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"意见反馈";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"您有什么问题或建议想问我们？";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    // 创建textView
    UITextView *textView =[[UITextView alloc]initWithFrame:CGRectMake(15 * SCREENH6,40 * SCREENH6,SCREEN_WIDTH - 30 * SCREENH6,300 * SCREENH6)];
    textView.backgroundColor= [UIColor whiteColor];
    textView.text = @"感谢您提下宝贵的建议";
    textView.font = [UIFont systemFontOfSize:14 * SCREENH6];
    textView.textColor = ColorWithRGB(0xbebebe);
    textView.delegate = self;
    
    self.textView = textView;
    [self.view addSubview:self.textView];
    
    
    UIButton * finishButton = [UIButton new];
    
    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishButton setBackgroundColor:MAINCOLOR];
    [finishButton addTarget:self action:@selector(clickFinishButton) forControlEvents:UIControlEventTouchUpInside];
    finishButton.titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    
    self.finishButton = finishButton;
    
    [self.view addSubview:self.finishButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    
    [self.finishButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-40 * SCREENH6);
        make.bottom.mas_equalTo(self.view).offset(-190 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
}


- (void)clickFinishButton{
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
   
    NSString * token = [userDefaults objectForKey:@"token"];
    
    NSDictionary * dict = @{@"token":token,@"content":self.textView.text};
    
    [[NetworkTool sharedTools] POST:@"/index/Note/feedback" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"aaaa%@",responseObject);
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      //                                                                          NSLog(@"action = %@", action);
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    
    if(textView.text.length < 1){
        textView.text = @"感谢您提下宝贵的建议";
        textView.textColor = ColorWithRGB(0xbebebe);
    }else{
        
        [textView resignFirstResponder];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"感谢您提下宝贵的建议"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.textView resignFirstResponder];
//    [self.textView endEditing:YES];
}



- (void)backToView{

    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
